package com.cc.excel.poi;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;

public class Test01 {
    public static void main(String[] args) throws IOException {
        //1.创建电子表格文档对象
        String filePath="/Users/mr.chan/Desktop/kate.xls";//文件路径
        HSSFWorkbook workbook = new HSSFWorkbook();//创建Excel文件(Workbook)
        //2.创建电子表格 sheet
        HSSFSheet sheet = workbook.createSheet();//创建工作表(Sheet)
       //3.设置电子表格的名称
        sheet = workbook.createSheet("Test");//创建工作表(Sheet)
        //设置框
        sheet.setColumnWidth(0, 31 * 256);
       
        //4.创建单元格
        HSSFRow row = sheet.createRow(0);// 创建行,从0开始
        //设置高
        row.setHeightInPoints(50);
        
         HSSFCell cell = row.createCell(0);// 创建行的单元格,也是从0开始
         //设置单元格样式
         HSSFCellStyle style=workbook.createCellStyle();
         
         
         //设置字体
         HSSFFont font = workbook.createFont();
         font.setFontName("华文行楷");//设置字体名称
         font.setFontHeightInPoints((short)28);//设置字号
//         font.setColor(HSSFColor.RED.index);//设置字体颜色
        font.setColor(Font.COLOR_RED);//设置字体颜色
        style.setFont(font);
         //设置边框
         style.setFillBackgroundColor((short)13);
         style.setBorderBottom(BorderStyle.THIN);
         style.setBorderTop(BorderStyle.THIN);
         style.setBorderLeft(BorderStyle.THIN);
         style.setBorderRight(BorderStyle.THIN);
         
       //合并列
         CellRangeAddress region=new CellRangeAddress(1, 3, 1, 4);
         sheet.addMergedRegion(region);
         
         
        cell.setCellStyle(style);
        cell.setCellValue("李志伟");// 设置单元格内容`

        //5.创建IO流
        FileOutputStream out = new FileOutputStream(filePath);
        workbook.write(out);//保存Excel文件
        out.close();//关闭文件流
        System.out.println("OK!");
        
    }
}
